package Reeks5.VanRansbeeck.Kevin.View;

import Reeks5.VanRansbeeck.Kevin.Controller.IntegerVerifier;
import Reeks5.VanRansbeeck.Kevin.Controller.PokerButtonListener;
import Reeks5.VanRansbeeck.Kevin.Model.PokerSpeler;
import Reeks5.VanRansbeeck.Kevin.Model.States.Player.PlayerStateContext;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public final class PokerToolbar extends GameToolbar {

    JLabel lblSaldo;
    FoldTimer tmrFold;
    SplashButton btnCheck, btnCall;
    JSpinner spnBet, spnRaise;
    SplashButton btnBet, btnRaise, btnFold, btnAllin;
    PlayerStateContext context;
    
    public PokerToolbar(GameGUI c) {
        super(c);
        this.setLayout(new GridLayout(0,2,0,10));
        context = getGameGUI().getSpeler().getStateContext();
        disableAll();
    }

    @Override
    void setButtons() {
        PokerButtonListener buttonListener = new PokerButtonListener(getGameGUI(), this);

        lblSaldo = new JLabel("€ " + getGameGUI().getSpeler().getSaldo());
        lblSaldo.setFont(new Font("Serif", Font.BOLD, 24));
        add(lblSaldo);
        
        tmrFold = new FoldTimer(getGameGUI());
        add(tmrFold); // pas starten als kaarten gedrawd zijn
        
        btnCheck = new SplashButton("Check"); 
        btnCheck.addActionListener(buttonListener);
        add(btnCheck);
        
        btnCall = new SplashButton("Call"); 
        btnCall.addActionListener(buttonListener);
        add(btnCall);
        
        spnBet = new JSpinner(
                     new SpinnerNumberModel(parent.getSpel().getSmallblind() * 2, parent.getSpel().getSmallblind() * 2, parent.getSpeler().getSaldo(), parent.getSpel().getSmallblind() * 2)); 
        spnBet.setFont(new Font("Serif", Font.BOLD, 24));
        spnBet.setInputVerifier(new IntegerVerifier());
        add(spnBet);
        
        btnBet = new SplashButton("Bet");
        btnBet.addActionListener(buttonListener);
        add(btnBet);
        
        spnRaise = new JSpinner(
                     new SpinnerNumberModel(parent.getSpel().getSmallblind() * 2, parent.getSpel().getSmallblind() * 2, parent.getSpeler().getSaldo(), parent.getSpel().getSmallblind() * 2)); 
        spnRaise.setFont(new Font("Serif", Font.BOLD, 24));
        spnRaise.setInputVerifier(new IntegerVerifier());
        add(spnRaise);
        
        btnRaise = new SplashButton("Raise");
        btnRaise.addActionListener(buttonListener);
        add(btnRaise);
        
        btnFold = new SplashButton("Fold");
        btnFold.addActionListener(buttonListener);
        add(btnFold);
        
        btnAllin = new SplashButton("All-In");
        btnAllin.addActionListener(buttonListener);
        add(btnAllin);
        
        comps.add(lblSaldo); comps.add(tmrFold); comps.add(btnCheck); comps.add(btnCall); comps.add(spnBet); comps.add(spnRaise); comps.add(btnBet); comps.add(btnRaise); comps.add(btnFold); comps.add(btnAllin);
    }
    
    
    public void disableAll() {
        for(JComponent c : comps) {
            c.setEnabled(false);
        }
    }
    
    public int getBetValue() {
        try {
            return Integer.parseInt(spnBet.getValue().toString());
        } catch (Exception e) {
            return 0;
        }
    }
    public int getRaiseValue() {
        try {
            return Integer.parseInt(spnRaise.getValue().toString());
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public void toggleActionButtons() {
        disableAll();
        System.out.println("Updating zE buttons");
        PokerSpeler s = (PokerSpeler)parent.getSpeler();
        if(s.canRaise()) {
            btnRaise.setEnabled(true);
            spnRaise.setEnabled(true);
        }
        if(s.canFold()) {
            btnFold.setEnabled(true);
        }
        if(s.canBet()) {
            spnBet.setEnabled(true);
            btnBet.setEnabled(true);
        } 
        if(s.canCall()) {
            btnCall.setEnabled(true);
        }
        if(s.canCheck()) {
            btnCheck.setEnabled(true);
        }
        if(s.canAllin()) {
            btnAllin.setEnabled(true);
        }
    }
}
